package com.graduationproject.examonline.teacher.service;

import com.graduationproject.examonline.exam.entity.Exam;
import com.graduationproject.examonline.exam.mapper.ExamMapper;
import com.graduationproject.examonline.teacher.entity.Teacher;
import com.graduationproject.examonline.teacher.mapper.TeacherMapper;
import com.graduationproject.examonline.utils.MD5Utils;
import com.graduationproject.examonline.utils.Result;
import com.graduationproject.examonline.utils.SingelKey;
import com.graduationproject.examonline.utils.Token;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ExamMapper examMapper;

    public Result login(String teacherId, String teacherPassword) {
        Result result = null;
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            String masterPasswordMD5 = MD5Utils.stringToMD5(teacherPassword);
            params.put("teacherId", teacherId);
            params.put("teacherPassword", masterPasswordMD5);
            List<Teacher> record = teacherMapper.findTeacherByIdAndPassword(params);
            if (record.size() > 0) {
                Map<String, String> info = new HashMap<String, String>();
                info.put("userId", teacherId);
                info.put("userName", record.get(0).getTeach_name());
                info.put("identity", "teacher");

                long overDate = new Date().getTime() + 1 * 60 * 60 * 1000;
                String token = Token.createToken(teacherId, "teacher", record.get(0).getTeach_name(), new Date(overDate), SingelKey.getInstance().getKey());
                result = new Result(0, 200, "登录成功", token, info);
                return result;
            }
            return result = new Result(1, 200, "登录失败，密码错误");
        } catch (MyBatisSystemException e) {
            e.printStackTrace();
            return new Result(1, 200, "登录失败，检查身份是否正确");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 200, "登录失败，服务器出错");
        }
    }

    public Result updatePassword(Map<String, Object> params) {
        try {
            Map<String, Object> nextParams = new HashMap<String, Object>();
            String prePassword = (String) params.get("pre_password");
            nextParams.put("teacherId", params.get("teach_id"));
            nextParams.put("teacherPassword", MD5Utils.stringToMD5(prePassword));
            List<Teacher> record = teacherMapper.findTeacherByIdAndPassword(nextParams);
            if (record.size() > 0) {
                Map<String, Object> nextParams2 = new HashMap<String, Object>();
                nextParams2.put("teach_id", params.get("teach_id"));
                nextParams2.put("teach_password", MD5Utils.stringToMD5((String) params.get("next_password")));
                int amount = teacherMapper.updateById(nextParams2);
                if (amount > 0) {
                    return new Result(0, 200, "ok");
                } else {
                    return new Result(1, 200, "未修改任何记录");
                }
            } else {
                return new Result(1, 200, "密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result updateName(Map<String, Object> params) {
        try {
            int amount = teacherMapper.updateById(params);
            if (amount > 0) {
                return new Result(0, 200, "ok");
            } else {
                return new Result(0, 200, "error");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result getUserInfo(String teach_id) {
        try {
            List<Teacher> teachers = teacherMapper.getUserInfo(teach_id);
            if (teachers.size() > 0) {
                return new Result(0, 200, "ok", teachers.get(0));
            } else {
                return new Result(1, 200, "ok");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result getAllUserInfo() {
        try {
            List<Teacher> teacherList = teacherMapper.getAllUserInfo();
            return new Result(0, 200, teacherList);
        } catch (Exception e) {
            return new Result(1, 500, "error");
        }
    }

    public Result insertTeacher(Map<String, Object> p) {
        try {
            p.put("teach_password", MD5Utils.stringToMD5((String) p.get("teach_password")));
            teacherMapper.insertTeacher(p);
            return new Result(0, 200, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result updateById(Map<String, Object> p) {
        try {
            System.out.println(p);
            if (p.get("teach_password") != null) {
                p.put("teach_password", MD5Utils.stringToMD5((String) p.get("teach_password")));
            }
            if(p.get("deleted") != null){
                if ((int) p.get("deleted") == 1) {
                    p.put("next_teach_id", p.get("teach_id") + "_deleted_" + new Date().getTime());
                }
            }
            teacherMapper.updateById(p);
            if (p.get("next_teach_id") != null) {
                List<Exam> examList = examMapper.getExamListByTeachId((String) p.get("teach_id"));
                for (int i = 0; i < examList.size(); i++) {
                    Map<String, Object> p2 = new HashMap<String, Object>();
                    p2.put("exam_id", examList.get(i).getExam_id());
                    p2.put("teach_id", p.get("next_teach_id"));
                    examMapper.updateExamById(p2);
                }
            }
            return new Result(0, 200, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

}
